package com.ksyun.campus.dataserver.bootstrap;

import com.ksyun.campus.common.constant.Sys;
import com.ksyun.campus.common.domain.DataServer;

import java.util.UUID;

/**
 * @author zhaozhikai
 * @date 2023/8/7 1:38
 */
public class DataServerInstance {
    private static DataServer dataServer;
    private static String DATA_PATH;


    private DataServerInstance() {
    }

    public static void init(String ipAddress, int port, int capacity, String rack, String zone) {
        if (dataServer == null) {
            synchronized (DataServerInstance.class) {
                if (dataServer == null) {
                    DataServerInstance.dataServer = new DataServer();
                    dataServer.setId(UUID.randomUUID().toString());
                    dataServer.setCapacity(capacity);
                    dataServer.setPort(port);
                    dataServer.setIp(ipAddress);
                    dataServer.setRack(rack);
                    dataServer.setZone(zone);
                    dataServer.setFileTotal(0);
                    dataServer.setUseCapacity(0);
                    DataServerInstance.DATA_PATH = "data" + Sys.SEPARATOR +  ipAddress + "-" + port;
                }
            }
        }
    }
    public static String getDataPath() {
        return DATA_PATH;
    }

    public static DataServer getDataServer() {
        return dataServer;
    }

    /**
     * 更新数据节点信息
     * @param plusFileNum 添加的文件数量
     * @param plusFileSize 新增文件容量
     */
    public static void updateInfo(int plusFileNum, int plusFileSize){
        DataServerInstance.dataServer.setFileTotal(DataServerInstance.dataServer.getFileTotal() + plusFileNum);
        DataServerInstance.dataServer.setUseCapacity(DataServerInstance.dataServer.getUseCapacity() + plusFileSize);
    }
}
